package com.sdp.core.bizc.listener;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.sdp.core.bizc.annotation.Cache;
import com.sdp.core.bizc.annotation.CacheKey;
import com.sdp.core.bizc.vo.CacheInfo;

@Component
public class CacheListener {
	private Map<String,CacheInfo> cacheMap = new HashMap<>();
	
	public void getCacheKeysWithAnnotation(ApplicationContext applicationContext) {
		Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Cache.class);
		for(Object bean : beans.values()){
			
			Method[] methods = bean.getClass().getDeclaredMethods();
			if(methods != null){
				CacheKey cacheKey = null;
				for(Method method : methods){
					cacheKey = method.getAnnotation(CacheKey.class);
					if(cacheKey!=null) {
						CacheInfo cacheInfo = new CacheInfo();
						cacheInfo.setBean(bean);
						cacheInfo.setMethod(method);
						cacheInfo.setKeyName(cacheKey.name());
						cacheMap.put(cacheKey.name(), cacheInfo);
					}
					
				}
			}
		}
	}



	public Map<String, CacheInfo> getCacheMap() {
		return cacheMap;
	}



	public void setCacheMap(Map<String, CacheInfo> cacheMap) {
		this.cacheMap = cacheMap;
	}

	
	
}
